package com.mianyang.zhangsan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.dto.SecAccountRole;
import com.mianyang.zhangsan.model.entity.SecAccount;
import com.mianyang.zhangsan.model.entity.SecAuthority;
import com.mianyang.zhangsan.service.ISecAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/account")
public class SecAccountController extends BaseController {
    @Autowired
    private ISecAccountService accountService;

    /**
     * 查询下级用户
     */
    @GetMapping("/lower/{userId}")
    public R queryLower(@PathVariable("userId") Long userId) {
        List<SecAccount> secAccounts = accountService.selectLowerAccount(userId);
        return success("lower", secAccounts);
    }

    /**
     * 查询个人信息
     */
    @GetMapping("/self/{accountId}")
    public R querySelf(@PathVariable("accountId") Long accountId) {
        SecAccount account = accountService.selectSelfAccount(accountId);
        return success("self", account);
    }

    @GetMapping("/all")
    public R queryAccounts(SecAccount account,
                           @RequestParam(value = "current", defaultValue = "1") int current,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Page<SecAccount> accounts = accountService.selectAccounts(account, current, pageSize);
        return success("accounts", accounts);
    }

    /**
     * 修改个人信息
     */
    @PutMapping("/edit")
    public R editSelf(@RequestBody SecAccount account) {
        accountService.updateAccount(account);
        return success();
    }

    @GetMapping("/auth")
    public R queryCurrentAuthority() {
        List<SecAuthority> authorities = accountService.selectCurrentAccountAuthority();
        return success("authorities", authorities);
    }

    @GetMapping("/roles/{accountId}")
    public R queryAccountRoles(@PathVariable("accountId") Long accountId) {
        SecAccountRole accountRole = accountService.selectAccountRoles(accountId);
        return success("accountRole", accountRole);
    }
}
